package rp.lee.musicplayer.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rp.lee.model.MusicInfo;
import rp.lee.musicplayer.R;
import rp.lee.utils.CollectionUtils;
import rp.lee.utils.CommonUtils;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PlayItemsAdapter extends BaseAdapter {
	
	Context context; 
	List<MusicInfo> data = new ArrayList<MusicInfo>();
	Map<Integer, View> cache = new HashMap<Integer, View>();
	Map<Integer, Integer> idCache = new HashMap<Integer, Integer>();
	public void initCache() {
		int count = this.data.size();
		cache.clear();
		idCache.clear();
		for (int i = 0; i < count; i++) {
			/*View view = LayoutInflater.from(context).inflate(R.layout.playitems_listview_item, null);
			MusicInfo info = data.get(i);
			view.setTag(info);
			TextView title = (TextView) view.findViewById(R.id.title);
			TextView artist = (TextView) view.findViewById(R.id.artist);
			TextView album = (TextView) view.findViewById(R.id.album);
			TextView playtime = (TextView) view.findViewById(R.id.playtime);
			TextView mime_type = (TextView) view.findViewById(R.id.mime_type);
			TextView duration = (TextView) view.findViewById(R.id.duration);
			
			title.setText((i + 1) + "." + info.getName()*//** + ":" + info.get_id()*//*);
			artist.setText(info.getArtist());
			album.setText(info.getAlbum());
			playtime.setText(CommonUtils.parserNumber2Minute(info.getTime()));
			mime_type.setText(info.getType());
			duration.setText(CommonUtils.parserNumber2Capacity(info.getSize()));
			
			cache.put(i, view);
			idCache.put(info.get_id(), i);*/
		}
	}
	
	public void setData(List<MusicInfo> data) {
		this.data = data;
	}

	public int locatePosition(int musicId) {
		if(idCache.containsKey(musicId)) {
			return idCache.get(musicId);
		}
		return -1;
	}
	
	public View getView(int p) {
		if (cache.containsKey(p)) {
			return cache.get(p);
		}
		return null;
	}
	
	public PlayItemsAdapter(Context context, List<MusicInfo> data){
		this.context = context;
		if(CollectionUtils.isNotEmpty(data)) {
			this.data = data;
			initCache();
		}
	}
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return data.get(position).get_id();
	}
	
	@Override
	public boolean isEnabled(int position) {
		return true;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(this.cache.containsKey(position)) {
			return this.cache.get(position);
		}
		View view = LayoutInflater.from(context).inflate(R.layout.playitems_listview_item, null);
		MusicInfo info = data.get(position);
		view.setTag(info);
		TextView title = (TextView) view.findViewById(R.id.title);
		TextView artist = (TextView) view.findViewById(R.id.artist);
		TextView album = (TextView) view.findViewById(R.id.album);
		TextView playtime = (TextView) view.findViewById(R.id.playtime);
		TextView mime_type = (TextView) view.findViewById(R.id.mime_type);
		TextView duration = (TextView) view.findViewById(R.id.duration);
		
		title.setText((position + 1) + "." + info.getName()/** + ":" + info.get_id()*/);
		artist.setText(info.getArtist());
		album.setText(info.getAlbum());
		playtime.setText(CommonUtils.parserNumber2Minute(info.getTime()));
		mime_type.setText(info.getType());
		duration.setText(CommonUtils.parserNumber2Capacity(info.getSize()));
		
		cache.put(position, view);
		idCache.put(info.get_id(), position);
		return this.cache.get(position);
	}
}